<?php

class AdminNewsEditController extends AdminIndexController 
{
    public function prepare()
    {
        // Handle POST::NewsSaveAction
        ApplicationController::attachEventListener($this, array($_POST, 'NewsSaveAction'));
        
        // Get news data
        $NewsModel = new NewsModel();
        $news = $NewsModel->get(Env::getParam('id'));
            
        if(!$news) {
            throw new HTTP404Exception();
        }
        
        $this->view->assign('news', $news);
    }
    
    public function exec()
    {
        if (Env::post('id')) {
            $news = new News(Env::post('id'), Env::post('title'), null, Env::post('description'), Env::post('image'), Env::post('status'), null, Env::post('link'));
            
            $nm = new NewsModel(DbConnection::COMMON_SPACE, true);
            $nm->edit($news);
            
            Location::go('/admin/news/edit/'.Env::post('id').'/');
        }
        Location::go('/admin/news/');
    }
}

?>